package jEDF.JEDF;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.Timer;

import javax.swing.*;

import jEDF.EDF.*;
import jEDF.Exceptions.*;
import jEDF.Hypnogram.*;
import jEDF.Montage.*;
import jEDF.PreciseScoring.*;
import jEDF.Resources.*;

/**
 *
 * <p>Titre : jEDF</p>
 * <p>Description : Java European Data Format Viewer and Analyser</p>
 * <p>Author : Nizar Kerkeni</p>
 * <p>Copyright : Copyright (c) 2003-2006</p>
 * <p>Version 2.0</p>
 */
public class JEDFMainWindow extends JFrame {

    private static String version = "jEDF 2.0.1";
    private JEDFMenu mainMenu = null;
    private JPanel jpToolBar = new JPanel();
    private JToolBar jtbFile = new JToolBar();
    private JButton jbOpen = new JButton();
    private JButton jbClose = new JButton();
    private JButton jbInfos = new JButton();
    private JToolBar jtbEDF = new JToolBar();
    private JButton jbFirst = new JButton();
    private JButton jbLast = new JButton();
    private JButton jbPreviousRecord = new JButton();
    private JButton jbNextRecord = new JButton();
    private JToggleButton jbPlay = new JToggleButton();
    private MontageToolbar montageToolbar = null;
    private Timer playTimer = null;
    private GridBagLayout gbl = new GridBagLayout();
    private GridBagConstraints gbc = new GridBagConstraints();
    private JToolBar jtbHypnogram = new JToolBar();
    private JButton jbLoadHypnogram = new JButton();
    private JButton jbCloseHypnogram = new JButton();
    private JButton jbExportToTextHypnogram = new JButton(getLocalizedString("Export to Text"));
    private String startText = getLocalizedString("Ready whenever you are !");
    private JLabel jlStatusBar = new JLabel(startText);
    private EDFFile edfFile = null;
    private JEDFHypnogram hypnogram = null;
    private JEDFSignalCaption signalCaption = null;
    private Polysomnograph polysomnograph = null;
    private JEDFStagesPanel stagesPanel = null;
    private PreciseScoring preciseScoring = null;
    private JPanel clientArea = new JPanel(new BorderLayout());
    private JPanel centerArea = null;
    private JPanel hypnoAndStagesPanel = new JPanel(new BorderLayout());
    private static ResourceBundle resource = new MyResource_EN();
    private JEDFMainWindow self = this;
    private JToolBar jtbDisplay = new JToolBar();
    private JButton jbZoomIn = new JButton();
    private JButton jbZoomOut = new JButton();
    private JButton jbResetZoom = new JButton();
    private double captionProportion = 0.05;
    private boolean signalIsInverted;

    //IASx01 by FO - INICIO - Inclusão do Auto Scoring
    private AutoScoring autoscoring = null;
    //IASx01 by FO - FIM - Inclusão do Auto Scoring

    /**
     * create the main window of the program
     */
    public JEDFMainWindow() {
        super(version);

        try {
            jbInit();
            updateTexts();
            addListeners();
            updatePermissions();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() {

        stagesPanel = new JEDFStagesPanel();
        hypnogram = new JEDFHypnogram(this, stagesPanel);
        polysomnograph = new Polysomnograph(this, hypnogram);
        signalCaption = new JEDFSignalCaption(polysomnograph);
        montageToolbar = new MontageToolbar(this, polysomnograph);
        preciseScoring = new PreciseScoring(this, polysomnograph, hypnogram);
        mainMenu = new JEDFMenu(this, polysomnograph, hypnogram, montageToolbar, preciseScoring);
        this.setIconImage(Toolkit.getDefaultToolkit().createImage(JEDFMainWindow.class.getResource(
                "/jEDF/icons/jEDFSmallLogo.png")));
        jbOpen.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/openFile.png")));
        jbClose.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/closeFile.png")));
        jbInfos.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/header.png")));
        jbFirst.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/first.png")));
        jbPreviousRecord.setIcon(new ImageIcon(JEDFMainWindow.class.getResource(
                "/jEDF/icons/prev.png")));
        jbNextRecord.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/next.png")));
        jbLast.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/last.png")));
        jbPlay.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/play.png")));
        jbZoomIn.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/zoomin.png")));
        jbZoomOut.setIcon(new ImageIcon(JEDFMainWindow.class.getResource("/jEDF/icons/zoomout.png")));
        jbResetZoom.setIcon(new ImageIcon(JEDFMainWindow.class.getResource(
                "/jEDF/icons/resetzoom.png")));
        jbLoadHypnogram.setIcon(new ImageIcon(JEDFMainWindow.class.getResource(
                "/jEDF/icons/openHypno.png")));
        jbCloseHypnogram.setIcon(new ImageIcon(JEDFMainWindow.class.getResource(
                "/jEDF/icons/closeHypno.png")));

        jlStatusBar.setBorder(BorderFactory.createEtchedBorder());

        jtbFile.add(jbOpen);
        jtbFile.add(jbClose);
        jtbFile.add(jbInfos);
        jtbDisplay.add(jbZoomIn);
        jtbDisplay.add(jbResetZoom);
        jtbDisplay.add(jbZoomOut);
        DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().
                addKeyEventPostProcessor(new KeyboardEventCatcher());

        jtbEDF.add(jbFirst);
        jtbEDF.add(jbPreviousRecord);
        jtbEDF.add(polysomnograph.getNavigationSlider());

        jtbEDF.add(jbNextRecord);
        jtbEDF.add(jbLast);
        jtbEDF.addSeparator();
        jtbEDF.add(jbPlay);
        jtbEDF.addSeparator();
        jtbHypnogram.add(jbLoadHypnogram);
        jtbHypnogram.add(jbCloseHypnogram);
        jtbHypnogram.add(jbExportToTextHypnogram);

        jtbEDF.add(polysomnograph.getEpochDurationComboBox());

        clientArea.setBackground(Color.white);

        setSize(1024, 768);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clientArea.add(jlStatusBar, BorderLayout.SOUTH);

        jpToolBar.add(jtbFile);
        jpToolBar.add(jtbEDF);
        jpToolBar.add(jtbDisplay);
        jpToolBar.add(jtbHypnogram);
        jpToolBar.add(montageToolbar);

        JPanel northArea = new JPanel();
        northArea.setLayout(new BorderLayout());
        northArea.add(mainMenu, BorderLayout.NORTH);
        northArea.add(jpToolBar, BorderLayout.WEST);

        clientArea.add(northArea, BorderLayout.NORTH);

        centerArea = new JPanel(gbl);

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 8;
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 0.8;
        gbc.weightx = captionProportion;
        gbl.setConstraints(signalCaption, gbc);
        centerArea.add(signalCaption);

        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridy = 1;
        gbc.gridheight = 7;
        gbc.gridwidth = 9;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 0.79;
        gbc.weightx = 1.0 - captionProportion;
        gbl.setConstraints(polysomnograph, gbc);
        centerArea.add(polysomnograph);

        gbc.gridx = 1;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.gridheight = 1;
        gbc.gridwidth = 9;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 0.01;
        gbc.weightx = 1.0;

        gbl.setConstraints(preciseScoring, gbc);
        preciseScoring.setVisible(false);
        centerArea.add(preciseScoring);

        hypnoAndStagesPanel.add(stagesPanel, BorderLayout.NORTH);
        hypnoAndStagesPanel.add(hypnogram, BorderLayout.CENTER);

        gbc.gridx = 0;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.gridwidth = 10;
        gbc.gridheight = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 0.1;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.CENTER;

        gbl.setConstraints(hypnoAndStagesPanel, gbc);

        centerArea.add(hypnoAndStagesPanel);
        hypnoAndStagesPanel.setVisible(false);

        clientArea.add(centerArea, BorderLayout.CENTER);
        clientArea.setFocusable(true);

        setContentPane(clientArea);
    }

    public void quit() {
        System.gc();
        dispose();
        System.exit(0);
    }

    private void cancelPlayTimer() {
        if (jbPlay.isSelected()) {
            jbPlay.doClick();
        }
    }

    private void addListeners() {
        jbOpen.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                openEDFFile();
            }
        });

        jbClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                System.gc();
                close();
            }
        });

        jbFirst.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                cancelPlayTimer();
                goToFirstRecord();
            }
        });

        jbLast.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                cancelPlayTimer();
                goToLastRecord();
            }
        });

        jbPreviousRecord.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                cancelPlayTimer();
                goToPreviousRecord();
            }
        });
        jbNextRecord.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                cancelPlayTimer();
                goToNextRecord();
            }
        });

        jbInfos.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                new EDFInformationDialog(self);
            }
        });

        jbLoadHypnogram.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                new NewHypnogramDialog(self, polysomnograph, hypnogram, true);
                updatePermissions();
            }
        });

        jbCloseHypnogram.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                closeHypnogram();
            }
        });

        jbExportToTextHypnogram.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                exportHypnogramToText();
            }
        });

        jbZoomIn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                polysomnograph.zoomIn();
            }
        });

        jbZoomOut.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                polysomnograph.zoomOut();
            }
        });

        jbResetZoom.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                polysomnograph.zoomReset();
            }
        });

        clientArea.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent arg0) {
                KeyboardFocusManager keyboardFocusManager =
                        DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager();
                keyboardFocusManager.clearGlobalFocusOwner();
            }
        });

        jbPlay.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                if (jbPlay.isSelected()) {
                    if (playTimer == null) {
                        playTimer = new Timer();
                        playTimer.schedule(new ForwardPlay(), 1000, 1000);
                    }
                } else {
                    if (playTimer != null) {
                        playTimer.cancel();
                        playTimer = null;
                    }
                }
            }
        });
    }

    /**
     * enable or disable interface interactions according to the current state of
     * the program
     */
    public void updatePermissions() {
        if (edfFile == null) {
            jbOpen.setEnabled(true);
            jbClose.setEnabled(false);
            jtbEDF.setEnabled(false);
            jtbDisplay.setEnabled(false);
            polysomnograph.getNavigationSlider().setEnabled(false);
            polysomnograph.getEpochDurationComboBox().setEnabled(false);

            jbFirst.setEnabled(false);
            jbLast.setEnabled(false);
            jbPreviousRecord.setEnabled(false);
            jbNextRecord.setEnabled(false);
            jbInfos.setEnabled(false);

            jbZoomIn.setEnabled(false);
            jbZoomOut.setEnabled(false);
            jbResetZoom.setEnabled(false);

            jbLoadHypnogram.setEnabled(false);
            jbExportToTextHypnogram.setEnabled(false);
            jbCloseHypnogram.setEnabled(false);

            montageToolbar.setEnabled(false);
            jbPlay.setEnabled(false);

        } else {
            jbOpen.setEnabled(false);
            jbClose.setEnabled(true);
            jtbEDF.setEnabled(true);
            jtbDisplay.setEnabled(!edfFile.isItAHypnogramRecord());
            polysomnograph.getNavigationSlider().setEnabled(true);
            polysomnograph.getEpochDurationComboBox().setEnabled(true);

            jbZoomIn.setEnabled(true);
            jbZoomOut.setEnabled(true);
            jbResetZoom.setEnabled(true);

            jbFirst.setEnabled(!polysomnograph.isItTheBeginning());
            jbPreviousRecord.setEnabled(!polysomnograph.isItTheBeginning());

            jbLast.setEnabled(!polysomnograph.isItTheEnd());
            jbNextRecord.setEnabled(!polysomnograph.isItTheEnd());

            jbInfos.setEnabled(true);

            jbLoadHypnogram.setEnabled(hypnogram.isCleared());
            jbExportToTextHypnogram.setEnabled(!hypnogram.isCleared());
            jbCloseHypnogram.setEnabled(!hypnogram.isCleared());

            montageToolbar.setEnabled(!edfFile.isItAHypnogramRecord());
            jbPlay.setEnabled(!edfFile.isItAHypnogramRecord());
        }

        hypnoAndStagesPanel.setVisible(!hypnogram.isCleared());
        preciseScoring.setVisible(preciseScoring.canBeDisplayed());

        mainMenu.updatePermissions();
        updateStatusBar();
    }

    /**
     * close all opened files
     */
    public void close() {
        try {
            cancelPlayTimer();
            hypnogram.clear();
            if (edfFile != null) {
                edfFile.close();
                edfFile = null;
            }
            polysomnograph.clear();
            montageToolbar.clear();
            setTitle(version);
            System.gc();
        } catch (UserCancelException e) {
            // cancelled by user
        } finally {
            updatePermissions();
            repaint();
        }

    }

    private class ForwardPlay extends TimerTask {

        /* (non-Javadoc)
         * @see java.util.TimerTask#run()
         */
        public void run() {
            if (polysomnograph.isItTheEnd()) {
                cancelPlayTimer();
            } else {
                goToNextRecord();
            }
        }
    }

    private class KeyboardEventCatcher implements KeyEventPostProcessor {

        /* (non-Javadoc)
         * @see java.awt.KeyEventPostProcessor#postProcessKeyEvent(java.awt.event.KeyEvent)
         */
        public boolean postProcessKeyEvent(KeyEvent arg0) {
            if (edfFile == null) {
                return false;
            }
            if (DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow() != self) {
                return false;
            }

            if (arg0.getID() == KeyEvent.KEY_PRESSED) {
                switch (arg0.getKeyCode()) {

                    case KeyEvent.VK_LEFT:
                        goToPreviousRecord();
                        break;
                    case KeyEvent.VK_RIGHT:
                        goToNextRecord();
                        break;

                    case KeyEvent.VK_HOME:
                        goToFirstRecord();
                        break;
                    case KeyEvent.VK_END:
                        goToLastRecord();
                        break;

                    case KeyEvent.VK_UP:
                        preciseScoring.nextPosition();
                        break;
                    case KeyEvent.VK_DOWN:
                        preciseScoring.previousPosition();
                        break;
                    case KeyEvent.VK_SPACE:
                        preciseScoring.setCustomText();
                        break;
                }

                switch (arg0.getKeyChar()) {
                    case '.':
                    case 'm':
                    case 'M':
                        hypnogram.setStageMVT();
                        break;
                    case 'p':
                    case 'P':
                        preciseScoring.setStatePV();
                        break;
                    case 'f':
                    case 'F':
                    case 's':
                    case 'S':
                        preciseScoring.setStateFx();
                        break;
                    case 'd':
                    case 'D':
                    case 't':
                    case 'T':
                        preciseScoring.setStateST();
                        break;
                    case 'k':
                    case 'K':
                        preciseScoring.setStateKCX();
                        break;
                    case '+':
                    case '=':
                        preciseScoring.duplicatePreviousScore();
                        break;

                    case '0':
                        hypnogram.setStageWAKE();
                        break;
                    case '1':
                        hypnogram.setStage1();
                        break;
                    case '2':
                        hypnogram.setStage2();
                        break;
                    case '3':
                        hypnogram.setStage3();
                        break;
                    case '4':
                        hypnogram.setStage4();
                        break;
                    case '5':
                    case 'r':
                    case 'R':
                        hypnogram.setStageREM();
                        break;
                    case '6':
                    case 'u':
                    case 'U':
                        hypnogram.setStageUnscored();
                        break;
                    case '7':
                    case 'o':
                    case 'O':
                        preciseScoring.setStateVYO();
                        break;
                    case '8':
                    case 'c':
                    case 'C':
                        preciseScoring.setStateVYF();
                        break;
                    case '9':
                    case 'a':
                    case 'A':
                        preciseScoring.setStateART();
                        break;
                }
            }
            cancelPlayTimer();
            return false;
        }
    }

    private void openFile(File file) {
        try {

            EDFFile newEdfFile = new EDFFile(file);

            close();
            edfFile = newEdfFile;

            polysomnograph.load(edfFile);
            try {
                hypnogram.loadEDFFile(edfFile);
            } catch (UserCancelException e) {
                // cancelled by user
                //e.printStackTrace();
            }

            setTitle(version + " - " + edfFile.getFileName());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,
                    getLocalizedString("Unable to access the specified file."),
                    getLocalizedString("Error"), JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, getLocalizedString(
                    "The specified file does not appear to be\nan EDF standard compliant file."),
                    getLocalizedString("Error"), JOptionPane.ERROR_MESSAGE);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, getLocalizedString(
                    "I/O Error : please verify that you have the rights to access the specified file\nand that it is not in use by another program."),
                    getLocalizedString("Error"), JOptionPane.ERROR_MESSAGE);
        }
        updatePermissions();
        repaint();
    }

    private void goToNextRecord() {
        polysomnograph.incrementStartRecord();
    }

    /**
     * called whenever the record to be displayed has changed
     */
    public void displayedRecordModified() {
        if (edfFile == null && hypnogram.isCleared()) {
            return;
        }

        preciseScoring.setFocus(false);
        updateStatusBar();

        polysomnograph.repaint();
        preciseScoring.repaint();
        hypnogram.repaint();

        jbFirst.setEnabled(!polysomnograph.isItTheBeginning());
        jbPreviousRecord.setEnabled(!polysomnograph.isItTheBeginning());

        jbLast.setEnabled(!polysomnograph.isItTheEnd());
        jbNextRecord.setEnabled(!polysomnograph.isItTheEnd());
    }

    private void goToPreviousRecord() {
        polysomnograph.decrementStartRecord();
    }

    private void goToFirstRecord() {
        polysomnograph.firstStartSecond();
    }

    private void goToLastRecord() {
        polysomnograph.lastStartSecond();
    }

    private void updateStatusBar() {
        if (edfFile == null) {
            jlStatusBar.setText(startText);
            return;
        }

        int epochNumber = (int) Math.round((double) polysomnograph.getStartSecond()
                / (double) edfFile.getRecordDuration()); //(double)polysomnograph.getNbSecondsToDisplay());
        int totEpoch = (int) Math.round((double) edfFile.getTotalDuration()
                / (double) edfFile.getRecordDuration()); //(double)polysomnograph.getNbSecondsToDisplay());
        epochNumber++;

        Calendar epochTime = edfFile.getStartTime();
        DateFormat dateFormat = DateFormat.getDateTimeInstance();

        epochTime.add(Calendar.SECOND, polysomnograph.getStartSecond());
        String startTime = dateFormat.format(epochTime.getTime());

        dateFormat = DateFormat.getTimeInstance();
        epochTime.add(Calendar.SECOND, polysomnograph.getEpochDuration());
        String endTime = dateFormat.format(epochTime.getTime());

        String statusText = getLocalizedString("Epoch n° ") + epochNumber + "/"
                + totEpoch + "        " + startTime + " -> " + endTime;

        if (!hypnogram.isCleared()) {
            statusText += getLocalizedString("                 Hypnogram Epoch Duration : ")
                    + hypnogram.getEpochDuration();
        }

        jlStatusBar.setText(statusText);

    }

    /**
     * display a file chooser dialog for the user to open an edf file
     */
    public void openEDFFile() {
        JFileChooser chooser = new JFileChooser();

        String[] extensions = {"rec", "hyp", "edf"};

        chooser.setFileFilter(new EDFFileFilter(extensions, getLocalizedString("EDF files")));

        int returnVal = chooser.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            openFile(chooser.getSelectedFile());
        }
        updatePermissions();
    }

    /**
     * @return the currently opened EDFFile
     */
    public EDFFile getEDFFile() {
        return edfFile;
    }

    public JEDFHypnogram getHypnogram() {
        return hypnogram;
    }

    public Polysomnograph getPolysomnograph() {
        return polysomnograph;
    }

    public JLabel getStatusBar() {
        return jlStatusBar;
    }

    public void setStatusBar(JLabel jlStatusBar) {
        this.jlStatusBar = jlStatusBar;
    }



    /**
     * set the EDF signal status, inverted or not.
     * @param b boolean
     */
    public void setsignalInverted(boolean b) {
        signalIsInverted = b;
    }

    /**
     * get the EDF signal status, inverted or not.
     * @return boolean the status
     */
    public boolean getsignalInverted() {
        return signalIsInverted;
    }

    /**
     * close the hypnogram, causing it to be hidden
     */
    public void closeHypnogram() {
        try {
            hypnogram.clear();

        } catch (UserCancelException e) {
            // user has cancelled his action
        } finally {
            updatePermissions();
        }
    }

    //IASx01 by FO - INICIO - Inclusão do Auto Scoring
    public void startAutoScoring() {

        if(autoscoring == null)
            autoscoring = new AutoScoring(this);

        autoscoring.startAutoScoring();
        
    }

    //IASx01 by FO - FIM - Inclusão do Auto Scoring
    /**
     * display a file chooser dialog for the user to save the currently load hypnogram
     */
    public void exportHypnogramToText() {
        new HypnogramToTextDialog(this, hypnogram);
        updatePermissions();
    }

    /**
     * @param string the String to be localized
     * @return the localized String
     */
    public static String getLocalizedString(String string) {
        return resource.getString(string);
    }

    private void updateTexts() {
        jbOpen.setToolTipText(getLocalizedString("Open"));
        jbClose.setToolTipText(getLocalizedString("Close"));
        jbInfos.setToolTipText(getLocalizedString("Informations"));
        jbFirst.setToolTipText(getLocalizedString("First epoch"));
        jbPreviousRecord.setToolTipText(getLocalizedString("Previous epoch"));
        jbNextRecord.setToolTipText(getLocalizedString("Next epoch"));
        jbLast.setToolTipText(getLocalizedString("Last epoch"));
        jbZoomOut.setToolTipText(getLocalizedString("Zoom out"));
        jbZoomIn.setToolTipText(getLocalizedString("Zoom in"));
        jbResetZoom.setToolTipText(getLocalizedString("Reset zoom"));
        jbPlay.setToolTipText(getLocalizedString("Auto scroll"));

        jbLoadHypnogram.setToolTipText(getLocalizedString("Open hypnogram"));
        jbCloseHypnogram.setToolTipText(getLocalizedString("Close hypnogram"));
        jbExportToTextHypnogram.setText(getLocalizedString("Export to Text"));

        startText = getLocalizedString("Ready whenever you are !");
        montageToolbar.updateTexts();

        hypnogram.updateTexts();
        polysomnograph.updateTexts();
    }

    /**
     * switch the program to the english language
     */
    public void switchToEnglish() {
        resource = new MyResource_EN();
        updateTexts();
        updateStatusBar();
    }

    /**
     * switch the program to the french language
     */
    public void switchToFrench() {
        resource = new MyResource_FR();
        updateTexts();
        updateStatusBar();
    }
}
